{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1e81d96",
   "metadata": {},
   "source": [
    "-------\n",
    "\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b25979f0",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "## 关于麦克风包安装\n",
    "\n",
    "## 优化ASR(自动语音识别)机器人\n",
    "\n",
    "## 连接图灵机器人(不用自己的字典)\n",
    "\n",
    "## 语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d2b2d61",
   "metadata": {},
   "source": [
    "# 关于麦克风包安装"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbe67cf8",
   "metadata": {},
   "source": [
    "* 安装 speech_recognition\n",
    "> pip install SpeechRecognition\n",
    "\n",
    "* 安装 PyAudio\n",
    "> Not found name \"PyAudio\" moudle\n",
    "\n",
    "> pip install pipwin\n",
    "\n",
    "> pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb7aba7a",
   "metadata": {},
   "source": [
    "# 优化ASR(自动语音识别)机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cbd1bce7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#百度密钥\n",
    "API_KEY = 'm1MkWxd8SF7ZSvE7KcBsE4GNwiUAgHxZ',
    "SECRET_KEY = 'nbRRYkiywvEEi59MxkEJVjR799fNgH_y'
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "eccab383",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aed5e1b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.0afa975b98d791e0b04e6b8a784ec294.2592000.1655346895.282335-19331335'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f9f96919",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "41789939",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.263573\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'好你好。'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "asr_output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79f07c7e",
   "metadata": {},
   "source": [
    "# 连接图灵机器人\n",
    "\n",
    "* API V2.0是基于图灵机器人平台语义理解、深度学习等核心技术，为广大开发者和企业提供的在线服务和开发接口。\n",
    "\n",
    "* 接口地址：\n",
    "> http://openapi.turingapi.com/openapi/api/v2\n",
    "\n",
    "* 请求方式:\n",
    "> POST\n",
    "\n",
    "* 请求参数：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d158b508",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "edea4ef9",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_KEY = \"d5d5bb66f66d4da8bedb378df7adfa70\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5449d061",
   "metadata": {},
   "outputs": [],
   "source": [
    "def TL_API(key,text):\n",
    "    TL_URL = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "\n",
    "    payload={\n",
    "        \"reqType\":0,\n",
    "        \"perception\": {\n",
    "            \"inputText\": {\n",
    "                \"text\": text\n",
    "            },\n",
    "        },\n",
    "        \"userInfo\": {\n",
    "            \"apiKey\": TL_KEY,\n",
    "            \"userId\": \"000001\"\n",
    "        }\n",
    "    }\n",
    "    res = requests.post(TL_URL,data = json.dumps(payload))\n",
    "    return res.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "620319db",
   "metadata": {},
   "source": [
    "## 接入对话机器人的流程\n",
    "\n",
    "> 1. 录制音频\n",
    "> 2. 语音识别--(百度ASR接口)---音频转文本信息\n",
    "> 3. 图灵API--(进行文字输入和文字回复)\n",
    "\n",
    "> 4. (待做) 语音合成（文字信息输出音频）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1d4932cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.590008\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\u3000\\u3000今天是星期一，我有三节课，分别是英语课、专业课、公选课。\\u3000\\u3000我不太喜欢英语课！\\u3000\\u3000但我喜欢专业课和选修课。'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 录制音频\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2. 语音识别\n",
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "\n",
    "# 3. 图灵API\n",
    "TL_API(TL_KEY,asr_output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3e4e7e7",
   "metadata": {},
   "source": [
    "# 语音合成测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a0176aa5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%E6%AC%A2%E8%BF%8E%E4%BD%BF%E7%94%A8%E7%99%BE%E5%BA%A6%E8%AF%AD%E9%9F%B3%E5%90%88%E6%88%90%E3%80%82\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.a665cef114548a24cbb6edcfaedd02b4.2592000.1655349836.282335-19331335&tex=%25E6%25AC%25A2%25E8%25BF%258E%25E4%25BD%25BF%25E7%2594%25A8%25E7%2599%25BE%25E5%25BA%25A6%25E8%25AF%25AD%25E9%259F%25B3%25E5%2590%2588%25E6%2588%2590%25E3%2580%2582&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "tts(xu_token,TEXT)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53ec2af9",
   "metadata": {},
   "source": [
    "# 在jupyter中播放音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e90e58ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/xuzhichao/anaconda3/lib/python3.7/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\n",
      "  warn(\"Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\", RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4534d318",
   "metadata": {},
   "source": [
    "# 智能对话机器人最终版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b1231cdd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.364203\n",
      "%E4%B8%8D%E9%95%BF%E7%8C%AA%E8%82%89%2C%E5%8F%AA%E9%95%BF%E5%B0%8F%E6%98%8E%E7%9A%84%E7%88%B8%E7%88%B8%E5%AF%B9%E5%B0%8F%E6%98%8E%E8%AF%B4%3A%E2%80%9C%E4%BD%A0%E7%9C%8B%E6%88%91%E8%BF%99%E4%B9%88%E5%A4%9A%E7%9A%84%E8%82%8C%E8%82%89%E3%80%82%E2%80%9D%E5%B0%8F%E6%98%8E%E8%AF%B4%3A%E2%80%9C%E9%82%A3%E4%BD%A0%E6%80%8E%E4%B9%88%E5%8F%AA%E9%95%BF%E9%B8%A1%E8%82%89%E4%B8%8D%E9%95%BF%E7%8C%AA%E8%82%89%E5%91%A2%EF%BC%9F%E2%80%9D%E5%BC%84%E5%BE%97%E7%88%B8%E7%88%B8%E5%93%AD%E7%AC%91%E4%B8%8D%E5%BE%97%E3%80%82\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.8f5d0297d0a360ffaa42946d99cdd4c7.2592000.1655350947.282335-19331335&tex=%25E4%25B8%258D%25E9%2595%25BF%25E7%258C%25AA%25E8%2582%2589%252C%25E5%258F%25AA%25E9%2595%25BF%25E5%25B0%258F%25E6%2598%258E%25E7%259A%2584%25E7%2588%25B8%25E7%2588%25B8%25E5%25AF%25B9%25E5%25B0%258F%25E6%2598%258E%25E8%25AF%25B4%253A%25E2%2580%259C%25E4%25BD%25A0%25E7%259C%258B%25E6%2588%2591%25E8%25BF%2599%25E4%25B9%2588%25E5%25A4%259A%25E7%259A%2584%25E8%2582%258C%25E8%2582%2589%25E3%2580%2582%25E2%2580%259D%25E5%25B0%258F%25E6%2598%258E%25E8%25AF%25B4%253A%25E2%2580%259C%25E9%2582%25A3%25E4%25BD%25A0%25E6%2580%258E%25E4%25B9%2588%25E5%258F%25AA%25E9%2595%25BF%25E9%25B8%25A1%25E8%2582%2589%25E4%25B8%258D%25E9%2595%25BF%25E7%258C%25AA%25E8%2582%2589%25E5%2591%25A2%25EF%25BC%259F%25E2%2580%259D%25E5%25BC%2584%25E5%25BE%2597%25E7%2588%25B8%25E7%2588%25B8%25E5%2593%25AD%25E7%25AC%2591%25E4%25B8%258D%25E5%25BE%2597%25E3%2580%2582&per=1&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition # 语音音频录制模块\n",
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play # 播放音频模块\n",
    "from bdasr import fetch_token,asr # token 和语音识别模块\n",
    "from bdtts import tts # 语音合成模块\n",
    "\n",
    "# 1. 录制音频\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到 wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2. 语音识别\n",
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "\n",
    "# 3. 图灵API\n",
    "TL_result = TL_API(TL_KEY,asr_output)\n",
    "\n",
    "# 4. 语音合成 TTS -->产生result.wav\n",
    "tts(xu_token,TL_result)\n",
    "\n",
    "# 5. 在jupyter中进行音频播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a07d737a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
